package com.hongxun.design.jungong.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;

import com.hongxun.design.R;
import com.hongxun.design.activitys.BaseActivity;
import com.hongxun.design.sqlite.bean.Bj;
import com.hongxun.design.ui.ScannerActivity;
import com.hongxun.design.utils.SPUtils;
import com.hongxun.design.utils.StringUtil;
import com.pop.android.common.util.ToastUtils;

import java.util.UUID;

public class BjEditeActivity extends BaseActivity {

    Bj bj;
    EditText name, code, phone, address, old_code, old_num, old_count, factory_new, new_code, new_num, new_count, h, l;
    Spinner type, t;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bj_edite);
        bj = getIntent().getParcelableExtra("data");
        name = (EditText) findViewById(R.id.name);
        code = (EditText) findViewById(R.id.code);
        phone = (EditText) findViewById(R.id.phone);
        address = (EditText) findViewById(R.id.address);
        old_code = (EditText) findViewById(R.id.old_code);
        old_num = (EditText) findViewById(R.id.old_num);
        old_count = (EditText) findViewById(R.id.old_count);
        factory_new = (EditText) findViewById(R.id.factory_new);
        new_code = (EditText) findViewById(R.id.new_code);
        new_num = (EditText) findViewById(R.id.new_num);
        new_count = (EditText) findViewById(R.id.new_count);
        h = (EditText) findViewById(R.id.h);
        l = (EditText) findViewById(R.id.l);
        type = (Spinner) findViewById(R.id.type);
        t = (Spinner) findViewById(R.id.t);
        if (bj != null) {
            name.setText(StringUtil.getNull(bj.getName()));
            code.setText(StringUtil.getNull(bj.getCode()));
            address.setText(StringUtil.getNull(bj.getAddress()));
            if (!TextUtils.isEmpty(bj.getType())) {
                String bjType = StringUtil.getNull(bj.getType());
                String[] array = getResources().getStringArray(R.array.bj_type);
                for (int i = 0; i < array.length; i++) {
                    if (array[i].equals(bjType)) {
                        type.setSelection(i);
                        break;
                    }
                }
            }
            String bjCode = StringUtil.getNull(bj.getBj_code());
            old_code.setText(bjCode);
            if (bjCode.length() > 14) {//旧表条形码长度>14则直接截取截值
                old_num.setText(bjCode.substring(bjCode.length() - 13, bjCode.length() - 1));
            } else {
                old_num.setText(StringUtil.getNull(bj.getNum()));
            }
            //new
            phone.setText(StringUtil.getNull(bj.getPhone()));
            old_count.setText(StringUtil.getNull(bj.getOld_count()));
            factory_new.setText(StringUtil.getNull(bj.getFactory_name()));
            new_code.setText(StringUtil.getNull(bj.getBj_new_code()));
            new_num.setText(StringUtil.getNull(bj.getNew_num()));
            new_count.setText(StringUtil.getNull(bj.getNew_count()));
            h.setText(StringUtil.getNull(bj.getH()));
            l.setText(StringUtil.getNull(bj.getL()));
            if (!TextUtils.isEmpty(bj.getT_jx())) {
                String tJx = StringUtil.getNull(bj.getT_jx());
                String[] array = getResources().getStringArray(R.array.t);
                for (int i = 0; i < array.length; i++) {
                    if (array[i].equals(tJx)) {
                        t.setSelection(i);
                        break;
                    }
                }
            }
        } else {
            bj = new Bj();
            bj.setStatus("0");
        }
    }

    public void click(View v) {
        switch (v.getId()) {
            case R.id.back_btn:
                finish();
                break;
            case R.id.save:
                if (TextUtils.isEmpty(new_code.getText().toString())) {
                    ToastUtils.showToast(this, "新表计条码不能为空");
                    return;
                }
                bj.setName(name.getText().toString());
                bj.setOld_count(old_count.getText().toString());
                bj.setBj_new_code(new_code.getText().toString());
                bj.setType(type.getSelectedItem().toString());
                bj.setT_jx(t.getSelectedItem().toString());
                bj.setCode(code.getText().toString());
                bj.setGuid(UUID.randomUUID().toString());
                bj.setH(h.getText().toString());
                bj.setL(l.getText().toString());
                bj.setPhone(phone.getText().toString());
                bj.setNew_count(new_count.getText().toString());
                bj.setFactory_name(factory_new.getText().toString());
                bj.setNew_factory(factory_new.getText().toString());//新表计厂家
                bj.setFactory_new_code(new_num.getText().toString());//新表计出厂编号=新表计号
                bj.setNum(old_num.getText().toString());
                bj.setNew_num(new_num.getText().toString());
                //new
                bj.setCode(code.getText().toString());
                bj.setAddress(address.getText().toString());
                bj.setBj_code(old_code.getText().toString());
                String new_code = (String) SPUtils.get(this, "new_code", "");
                bj.setBx_new_code(new_code);
                bj.setFactory_code(old_num.getText().toString());//旧表计出厂编号=旧表号

                Intent i = new Intent();
                i.putExtra("data", bj);
                setResult(Activity.RESULT_OK, i);
                finish();
                break;
            case R.id.setpoint_button:
                startActivityForResult(new Intent(this, ScannerActivity.class), 1);
                break;
            case R.id.setpoint_button2:
                startActivityForResult(new Intent(this, ScannerActivity.class), 2);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK && data != null) {
            String code = data.getStringExtra("result");
            if (requestCode == 1) {
                old_code.setText(code);
                if (code.length() > 14)
                    old_num.setText(code.substring(code.length() - 13, code.length() - 1));
            } else if (requestCode == 2) {
                new_code.setText(code);
                if (code.length() > 14)
                    new_num.setText(code.substring(code.length() - 13, code.length() - 1));
            }
        }
    }
}
